<?php
// +----------------------------------------------------------------------
// | 知识星球[基于ThinkPHP5开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2019 abcblockchain.io
// +----------------------------------------------------------------------
// | 后台管理使用了HisiPHP
// +----------------------------------------------------------------------
// | Author: abcblockchain.io
// +----------------------------------------------------------------------
namespace app\common\model;

use think\Model;
use think\config;
use think\Db;

class PhoneCode extends Model
{
    // 短信平台url
    protected $http;
    // 短信平台账号
    protected $user;
    // 短信平台密码
    protected $pwd;

    public function __construct()
    {
        $this->http = Config::get('user.smshttp');

        $this->user = Config::get('user.smsuser');

        $this->pwd = Config::get('user.smspwd');
    }

    //短信发送
    public function send($phone, $uid = 0, $action)
    {
        // 随机6位验证码
        //$code = rand(100000, 999999);
        $code = 111111;

        $time = date("Y-m-d H:i");

        $strtime = strtotime($time);

        $data = array(
            'code' => $code,
            'uid' => $uid,
            'ctype' => $action,
            'status' => 0,
            'phone' => $phone,
            'created' => $strtime
        );

        // 更新之前已经过的短信
        $res = Db::name('phone_code')->where('phone', $phone)->where('ctype', $action)->where('status', 0)->find();
        if ($res) {
            Db::name('phone_code')->where('phone', $phone)->where('ctype', $action)->where('status', 0)->update(['status' => 2, 'updated' => $strtime]);

        }

        // 组装内容
        $message = '';
        switch ($action) {
            case 1:
                $message = '您注册账号的验证码是：' . $code . ',请确认是（' . $phone . '）本人操作,本次验证码10分钟内有效。';
                $data['ctype'] = 1;
                break;
            case 2:
                $message = '您找回密码的验证码是：' . $code . ',请确认是（' . $phone . '）本人操作,本次验证码10分钟内有效。';
                $data['ctype'] = 2;
                break;
            case 3:
                $message = '您重置支付密码的验证码是：' . $code . ',请确认是（' . $phone . '）本人操作,本次验证码10分钟内有效。';
                $data['ctype'] = 3;
                break;
            case 4:
                $message = '您抢红包的验证码是：' . $code . ',请确认是（' . $phone . '）本人操作,本次验证码10分钟内有效。';
                $data['ctype'] = 4;
                break;
            case 5:
                $message = '您提现的验证码是：' . $code . ',请确认是（' . $phone . '）本人操作,本次验证码10分钟内有效。';
                $data['ctype'] = 5;
                break;
        }

        $data['message'] = $message;
        $content = $data['message'];

        // 插入数据库，用来校验验证码
        if (Db::name('phone_code')->insert($data)) {
            // 发送
            //$re = $this->newSendSMS($phone, $content);
            $re = 1;
            if ($re) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * 发送短信
     * @param $uid
     * @param $pwd
     * @param $mobile
     * @param $content
     * @param string $time
     * @param string $mid
     * @return string
     */
    function sendSMS($mobile, $conten)
    {
        $mobile = substr($mobile, -11);
        //$re= $this->postSMS($this->http,$data);
        $smsapi = $this->http; //短信网关
        $user = $this->user; //短信平台帐号
        $pass = md5($this->pwd); //短信平台密码
        $content = $conten;//要发送的短信内容
        $phone = $mobile;
        $params = array(
            'u' => $user,
            'p' => $pass,
            'm' => $phone,
            'c' => $content
        );
        //$smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);
        $sendurl = $smsapi . "sms";
        $re = http($sendurl, $params);
        return $re;
    }


    /**
     * 校验短信验证码时间
     * @param $phone 手机号
     * @param $aid  动作id
     * @return bool 返回是否为真
     */
    public function regverifiTime($phone, $aid)
    {
        $res = Db::name('phone_code')->where('phone', $phone)->where('action', $aid)->order('id desc')->find();
        if ($res) {
            if ($res['created'] + 40 < time()) {
                return true;
            }
        } else {
            return true;
        }
        return false;
    }

    /**
     * 校验验证码
     * @param $phone 手机号
     * @param $aid 动作id
     * @param $code 验证码
     * @return bool
     * @throws \think\Exception
     */
    public function verifiregCode($phone, $aid, $code)
    {
        $res = Db::name('phone_code')->where('phone', $phone)->where('ctype', $aid)->where('code', $code)->where('status', 0)->order('id desc')->find();
        if ($res) {
            $time = time();
            if ($res['created'] + 600 < $time) {
                return false;
            }
            $update = Db::name('phone_code')->where('id', $res['id'])->update(['status' => 1, 'updated' => $time]);
            if ($update) {
                return true;
            }
        }
        return false;
    }

    function trimall($str)//删除空格
    {

        $oldchar = array(" ", "　", "\t", "\n", "\r");

        $newchar = array("", "", "", "", "");

        return str_replace($oldchar, $newchar, $str);

    }


}